<template>
  <div>
    <h2>随机点名</h2>
    <div class="box">
      <span>名字是：</span>
      <div class="qs">{{qs}}</div>
    </div>
    <div class="btns">
      <button @click="start" class="start">开始</button>
      <button @click="end" class="end">结束</button>
    </div>
  </div>
</template>

<script>
// 声明定时器
let timeA =''
export default {
  data (){
    return {
      qs:'武松',
      array :['武松','宋江','李逵','卢俊义','林冲','柴进','吴用','关胜','公孙胜','秦明','呼延灼']
    }
  },
  methods: {
    // 点击开始生成随机数arr下标赋值给qs
    // 通过永久定时器来一直生成
    start(){
      timeA = setInterval(() => {
       this.qs = this.array[parseInt(Math.random() * this.array.length)]
      }, 100)
      document.querySelector('.start').disabled= true
    },
    end(){
      clearTimeout(timeA)
      document.querySelector('.start').disabled= false
    }
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
}

h2 {
  text-align: center;
}

.box {
  width: 600px;
  margin: 50px auto;
  display: flex;
  font-size: 25px;
  line-height: 40px;
}

.qs {
  width: 450px;
  height: 40px;
  color: red;
}

.btns {
  text-align: center;
}

.btns button {
  width: 120px;
  height: 35px;
  margin: 0 50px;
}
</style>